package com.beerandjesus.sportofkings.handicapper.strategy.brisnet.singlefile;

import com.beerandjesus.sportofkings.handicapper.model.HorseAnalysisResultImpl;
import com.beerandjesus.sportofkings.handicapper.model.Workout;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.strategy.RacingCalculator;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class FastWorkoutsResolver {

    private RacingCalculator calc = new RacingCalculator();

    void resolveFastWorkouts(HorseAnalysisResultImpl result, Horse h) {
        List<String> fastWorks = new ArrayList<String>();
        for (Workout w : h.getWorkouts()) {
            if (isSharpWorkout(w)) {
                fastWorks.add(calc.distanceFromYardsShortNotation(w.getDistance()));
            }
        }
        result.setFastWorkouts(fastWorks);
    }

    boolean isSharpWorkout(Workout w) {
        String d = w.getDistance();
        String t = w.getTime();
        if (StringUtils.isEmpty(d) || StringUtils.isEmpty(t)) {
            return false;
        }
        // negative if a "bullet" work, we don't particularly care
        Float f = new BigDecimal(t).abs().floatValue();
        String dist;
        try {
            dist = calc.distanceFromYards(d);
        } catch (Exception e) {
            return false;
        }
        if (dist.equals("4 Furlongs") && f.compareTo(new Float(48.0)) < 0) {
            return true;
        }
        if (dist.equals("5 Furlongs") && f.compareTo(new Float(60.0)) < 0) {
            return true;
        }
        if (dist.equals("6 Furlongs") && f.compareTo(new Float(73.0)) < 0) {
            return true;
        }
        return dist.equals("7 Furlongs") && f.compareTo(new Float(87.0)) < 0;
    }

}
